import { postAutohorityApi } from '@/services/api';
import {
  ModalForm,
  ProFormInstance,
  ProFormSelect,
} from '@ant-design/pro-components';
import { Form, message } from 'antd';
import { isEqual } from 'lodash';
import React, { useRef } from 'react';

interface Props {
  open?: boolean;
  info?: any;
  close?: () => void;
  onFinsh?: () => void;
  options?: any;
}
// 新增/编辑用户
const BindRuleDrawer: React.FC<Props> = ({
  open,
  close,
  info,
  onFinsh,
  options,
}) => {
  const [form] = Form.useForm<{ name: string; company: string }>();

  const formRef = useRef<ProFormInstance>();

  return (
    <ModalForm
      formRef={formRef}
      title={'配置规则'}
      form={form}
      open={open}
      width={600}
      onOpenChange={(visible) => {
        if (!visible) {
          close?.();
        }
      }}
      autoFocusFirstInput
      onFinish={async (values) => {
        try {
          const { msg } = await postAutohorityApi(info?.id, {
            ...values,
          });
          onFinsh?.();
          message.success(msg);
          // 不返回不会关闭弹框
          return true;
        } catch (error) {}
      }}
      initialValues={{
        ...info,
      }}
    >
      <ProFormSelect
        name="channel"
        label="渠道"
        // rules={[
        //   {
        //     required: true,
        //   },
        // ]}
        options={
          options
            ?.filter((_item) => _item?.type === 1)
            ?.map((e) => {
              return {
                value: e?.id,
                label: e?.title,
              };
            }) ?? []
        }
        placeholder="请选择绑定渠道"
        fieldProps={{
          showSearch: true,
        }}
      ></ProFormSelect>

      <ProFormSelect
        name="enterprise"
        label="公司"
        // rules={[
        //   {
        //     required: true,
        //   },
        // ]}
        options={
          options
            ?.filter((_item) => _item?.type === 2)
            ?.map((e) => {
              return {
                value: e?.id,
                label: e?.title,
              };
            }) ?? []
        }
        placeholder="请选择绑定公司"
        fieldProps={{
          showSearch: true,
        }}
      ></ProFormSelect>
    </ModalForm>
  );
};

export default React.memo(BindRuleDrawer, (prev, next) => isEqual(prev, next));
